package server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Form;

public class BtServer extends Form implements Runnable{

	LocalDevice mydev;
	String connStr="btspp://localhost:dcb91cb0638911dbbd130800200c9a66;authenticate=true;encrypt=true";
	
	DataInputStream in;
	DataOutputStream out;
		
	public BtServer( String title ) {
		 super( title ); 
	}
	
	public void start()
	{
		Thread thr = new Thread(this);
		thr.start();
	}
	public void run()
	{
		try
		{
			System.out.println("start BT Server..");
			// get Localdevice
			mydev= LocalDevice.getLocalDevice();
			mydev.setDiscoverable(DiscoveryAgent.GIAC);
		
			// Connection and Notifier
			StreamConnectionNotifier notifier;
			StreamConnection conn;
			
			notifier= (StreamConnectionNotifier)Connector.open(connStr);
			
			conn = notifier.acceptAndOpen();
			
			if (conn!=null)
			{		
				in= conn.openDataInputStream();
				out= conn.openDataOutputStream();
			
				// send messages
				for (int i = 0; i < 10; i++) 
				{
					out.writeUTF(i + ". msg form server" );
					String inMsg= in.readUTF();
					System.out.println(inMsg);
					this.append(inMsg);
				}				
			}				
		}
		catch (Exception e)
		{ 
			System.out.println(e.getMessage());
		}
	}
}